import json
from django.core import serializers
from django.db.models import Count, QuerySet
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, render_to_response
# Create your views here.
from django.views.decorators.csrf import csrf_exempt
from requests import Response
from rest_framework import viewsets

from news.models import News
from news.serializers import NewsSerializer


def home(request):
    return render(request, 'index.html')
def analysis_page(request):
    return render(request, 'analysis.html')

@csrf_exempt
def analysis(request):
    list = News.objects.all().values('college').annotate(Count=Count('college')).order_by()
    info = []
    for obj in list:
        print(obj)
        info.append(obj)
    result = {'status': 200, 'message': 'success', 'data': info}
    return HttpResponse(json.dumps(result,ensure_ascii=False), content_type="application/json,charset=utf-8")


def analysis_date(request):
    college_date = dict()

    list=News.objects.filter(college='计算机工程学院')
    time = processing_time(list)
    college_date['cec'] = time

    list=News.objects.filter(college='法学院')
    time = processing_time(list)
    college_date['soc'] = time

    list=News.objects.filter(college='航海学院')
    time = processing_time(list)
    college_date['nav'] = time

    list=News.objects.filter(college='轮机工程学院')
    time = processing_time(list)
    college_date['mei'] = time

    result = {'status': 200, 'message': 'success', 'data': college_date}
    return HttpResponse(json.dumps(result, ensure_ascii=False), content_type="application/json,charset=utf-8")
    # return JsonResponse(time, safe=False)

def processing_time(list):
    date = dict()

    date_list = []
    for x in range(2015,2020):
        date[x] = 0
    for obj in list:
        x = obj.pubdate
        x = x[0:4]
        if x == '2015':
            date[2015] += 1
        elif x == '2016':
            date[2016] += 1
        elif x == '2017':
            date[2017] += 1
        elif x == '2018':
            date[2018] += 1
        elif x == '2019':
            date[2019] += 1
    for x in range(2015,2020):
        obj = dict()
        obj['year'] = x
        obj['total'] = date[x]
        date_list.append(obj)
    print(date_list)
    return date_list


# class MySearchView(SearchView):
#     def build_page(self):
#         print('进入搜索页面：')
#         # 分页重写
#         context = super(MySearchView, self).extra_context()  # 继承自带的context
#         try:
#             page_no = int(self.request.GET.get('page', 1))
#         except Exception:
#             return HttpResponse("Not a valid number for page.")
#
#         if page_no < 1:
#             return HttpResponse("Pages should be 1 or greater.")
#         a = []
#         for i in self.results:
#             a.append(i.object)
#         paginator = Paginator(a, HAYSTACK_SEARCH_RESULTS_PER_PAGE)
#         # print("--------")
#         # print(page_no)
#         page = paginator.page(page_no)
#         print('搜索的商品信息：', page)
#         return (paginator, page)
#
#     def extra_context(self):
#         context = super(MySearchView, self).extra_context()  # 继承自带的context
#         context['title'] = '搜索'
#         return context